package com.Haitch.MaximWidget;

import java.util.HashMap;
import java.util.Map;

import com.Haitch.MaximWidget.R;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MaximWidgetConfigure extends Activity{
	public static final String PREFS_NAME = "com.Haitch.GnomeWidget";
	public static final String PREFS_UPDATE_GNOME_FIELD_PATTERN = "GNOME-%d";
	public static Map<String,String> GnomeDict = new HashMap<String,String>();
	private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // get any data we were launched with
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        if (extras != null) {
            appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            Intent cancelResultValue = new Intent();
            cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_CANCELED, cancelResultValue);
        } else {
            // only launch if it's for configuration
            // Note: when you launch for debugging, this does prevent this
            // activity from running. We could also turn off the intent
            // filtering for main activity.
            // But, to debug this activity, we can also just comment the
            // following line out.
            finish();
        }
        
        setContentView(R.layout.widgetconfigure);
        
        final SharedPreferences config = getSharedPreferences(PREFS_NAME, 0);
        final EditText gnome = (EditText) findViewById(R.id.editText1);
        gnome.setText(this.getString(R.string.txt_defaultmaxim));
        Button saveButton = (Button) findViewById(R.id.button1);
        saveButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //int updateRateSeconds = Integer.parseInt(updateRateEntry.getText().toString());
            	String gnomeText=gnome.getText().toString();

                // store off the user setting for update timing
                SharedPreferences.Editor configEditor = config.edit();

                configEditor.putString(String.format(PREFS_UPDATE_GNOME_FIELD_PATTERN, appWidgetId), gnomeText);
                configEditor.commit();

                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {

                    // tell the app widget manager that we're now configured
                    Intent resultValue = new Intent();
                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    setResult(RESULT_OK, resultValue);
                    
                    Intent widgetUpdate = new Intent();
                    widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                    widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
                    MaximWidgetConfigure.this.sendBroadcast(widgetUpdate);

                }

                // activity is now done
                finish();
            }
        });
        
    }
}
